<?php
namespace App\Common\Lib\Swoole\Channel;

use App\Common as com;

class Return_ extends com\Lib\LibBase {

	/**
	 * @var null|\Swoole\Lock
	 */
	private $lock = null;

	/**
	 * @var null|\Swoole\Channel
	 */
	private $channel = null;

	public function __construct() {
		$this->lock = new \Swoole\Lock( SWOOLE_MUTEX );
		$this->lock->lock();
		//
		$this->channel = new \Swoole\Channel( 1 );
	}

	/**
	 * 设置返回数据
	 * @param $value
	 * return void
	 */
	public function push( $value ) {
		$this->channel->push( $value );
		//
		$this->lock->unlock();
	}

	/**
	 * 获取数据
	 * @param $timeOutS
	 * @return mixed|null
	 */
	public function pop( $timeOutS = null ) {
		if( $this->lock->lockwait( $timeOutS ) === false ){
			// 进入锁超时
			return null;
		}
		// 取回数据
		$data =  $this->channel->pop();
		return $data !== false ? $data : null;
	}
}
